﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Visuals;
using System.Drawing;
using Silvermoon.Images;
using Silvermoon.Utils;
using Silvermoon.Core;
using Silvermoon.Animations;

namespace Silvermoon.Controls.Base
{
    public class ToolTipBase : EventControl
    {
        private BackgroundVisual background;
        private ImageVisual toolTipArrow;
        private Animation animation;

        public ToolTipBase()
            : base()
        {
            background = new BackgroundVisual { Texture = SysImages.ToolTip, Colors = ColorUtil.SingleColor(Color.White) };
            toolTipArrow = new ImageVisual { Image = SysImages.ToolTipArrowDown, Color = Color.White };
            Visuals = new Visual[] { background, toolTipArrow };
            TextColor = Color.White;
            Visible = false;
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.LayoutAppearance))
            {
                switch (Position)
                {
                    case ToolTipPosition.Above:
                        toolTipArrow.Image = SysImages.ToolTipArrowDown;
                        toolTipArrow.Bounds = Control.ScaledRect(64, Height - 16, 16, 16);
                        toolTipArrow.Visible = true;
                        background.Bounds = Control.ScaledRect(0, 0, Width, Height - 16 + 3);
                        break;

                    case ToolTipPosition.Below:
                        toolTipArrow.Image = SysImages.ToolTipArrowUp;
                        toolTipArrow.Bounds = Control.ScaledRect(64, 1, 16, 16);
                        toolTipArrow.Visible = true;
                        background.Bounds = Control.ScaledRect(0, 14, Width, Height - 16 + 3);
                        break;

                    default:
                        toolTipArrow.Visible = false;
                        break;
                }
            }
            base.OnLayout();
        }

        protected override void OnVisibleChanged()
        {
            if (Visible)
            {
                Screen.Attach(this);
                Screen.MouseFocusChanged += new EventHandler(OnScreenFocusChanged);
            }
            else
            {
                if (animation != null) animation.Abort();
                Screen.Detach(this);
                Screen.MouseFocusChanged -= OnScreenFocusChanged;
            }
            base.OnVisibleChanged();
        }

        private void ShowTransition()
        {
            Alpha = 0;
            Visible = true;
            if (animation != null) animation.Abort();
            animation = new Animation(700, 0, 255, 350);
            animation.Progress += (s, e) => Alpha = e.Value;
            animation.InvokeAsync();
        }

        public virtual void Show(Control anchor)
        {
            if (!Visible && Enabled)
            {
                Invalidate(InvalidationFlag.Appearance);
                PreLoad();
                Rectangle r = anchor.ToScreenBounds();
                if (r.Y >= (Height + 3))
                {
                    Position = ToolTipPosition.Above;
                    Location = new Point(r.X, r.Y - Height - 3);
                }
                else
                {
                    Position = ToolTipPosition.Below;
                    Location = new Point(r.X, r.Bottom);
                }
                Invalidate(InvalidationFlag.Appearance);
                ShowTransition();
            }

        }

        protected virtual void OnScreenFocusChanged(object sender, EventArgs e)
        {
            if (Screen.MessageServer.MouseFocusControl != null)
            {
                if (!this.HasFocus) Visible = false;
            }
        }

        public ToolTipPosition Position { get; private set; }

    }

    public enum ToolTipPosition
    {
        Above,
        Below
    }
}
